package com.rzjm.exercise.module.controller;

import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.Groups;
import com.rzjm.exercise.module.domain.Parameter;
import com.rzjm.exercise.module.domain.TemperatureLimit;
import com.rzjm.exercise.module.service.GroupsService;
import com.rzjm.exercise.module.service.ParameterService;
import com.rzjm.exercise.module.service.TemperatureLimitService;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import com.rzjm.exercise.sys.system.user.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 问卷列表
 *
 * @author mr.zhou
 * @email zhouwei_nice@163.com
 * @date 2021-11-5 12:36:23
 */
@Controller
@RequestMapping("/module/temperature/limit")
public class TemperatureLimitController extends BaseController {
    @Resource
    private TemperatureLimitService temperatureLimitService;
    @Resource
    private GroupsService groupsService;


    @Log("查询制粒机温度范围页面")
    @GetMapping()
    @RequiresPermissions("module:temperatureLimit:temperatureLimit")
    String temperatureLimit(Model model) {
        List<Groups> groups = groupsService.list();
        model.addAttribute("groups", groups);
        UserDO userDO = getUser();
        if(userDO.getUserType() > 1) {//集团用户
//            qw.eq("jituan_id", userDO.getGroupId());
            model.addAttribute("groupId", userDO.getGroupId());
        }
        return "module/temperaturelimit/index";
    }

    @Log("查询制粒机温度范围")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("module:temperatureLimit:temperatureLimit")
    public DataTableLayUI<TemperatureLimit> list(DataTableLayUI<TemperatureLimit> condition) {

        DataTableLayUI<TemperatureLimit> dt = temperatureLimitService.pageSearchLayUI(condition);
        List<TemperatureLimit> list = dt.getData();
        for (TemperatureLimit t : list){
            Groups groups = groupsService.findById(t.getGroupId());
            if (groups != null){
                t.setGroupName(groups.getGroupName());
            }
        }
        return dt;
    }


    @Log("新增制粒机温度范围页面")
    @GetMapping("/add")
    @RequiresPermissions("module:temperatureLimit:add")
    String add(Model model) {
        TemperatureLimit temperatureLimit = new TemperatureLimit();
        model.addAttribute("temperatureLimit", temperatureLimit);
        List<Groups> groups = groupsService.list();
        model.addAttribute("groups", groups);
        UserDO userDO = getUser();
        if(userDO.getUserType() > 1) {//集团用户
//            qw.eq("jituan_id", userDO.getGroupId());
            model.addAttribute("groupId", userDO.getGroupId());
        }
        return "module/temperaturelimit/detail";
    }


    @Log("编辑制粒机温度范围页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:temperatureLimit:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        TemperatureLimit temperatureLimit = temperatureLimitService.findById(id);
        model.addAttribute("temperatureLimit", temperatureLimit);
        List<Groups> groups = groupsService.list();
        model.addAttribute("groups", groups);
        UserDO userDO = getUser();
        if(userDO.getUserType() > 1) {//集团用户
//            qw.eq("jituan_id", userDO.getGroupId());
            model.addAttribute("groupId", userDO.getGroupId());
        }
        return "module/temperaturelimit/detail";
    }

    /**
     * 保存
     */
    @Log("新增制粒机温度范围")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:temperatureLimit:add")
    public R save(@Valid TemperatureLimit temperatureLimit) {
        if (temperatureLimitService.save(temperatureLimit)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("更新制粒机温度范围")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:temperatureLimit:edit")
    public R update(@Valid TemperatureLimit temperatureLimit) {
        temperatureLimitService.updateById(temperatureLimit);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除制粒机温度范围")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("module:temperatureLimit:remove")
    public R remove( Integer id) {
        if (temperatureLimitService.removeById(id)) {
            return R.ok();
        }
        return R.error();
    }
}
